
puts '[info] begin to generate rating file...'

if ARGV.length < 4
    puts '[error] program argument too less...'
    exit 0 
end
workingDir = ARGV[0]
sortedInput = ARGV[1]
ratingOutput = "#{sortedInput}_scaled"
scale_range = ARGV[2]
scale_rating = ARGV[3]
# 300-600-1800-3600-7200 1-2-3-4-5
rangeArr = scale_range.split('-')
ratingArr = scale_rating.split('-')
exit 0 if rangeArr.length != ratingArr.length

begin
    sortedInputFile = File.open("#{workingDir}/#{sortedInput}", 'r')
    ratingOutputFile = File.open("#{workingDir}/#{ratingOutput}", 'w')
    uid = 0
    pid = 0
    view = 0
    time = ''
    rating = 0
    sortedInputFile.each_line { |line|
        arr = line.split("\t")
        uid = arr[0].strip.to_i
        pid = arr[1].strip.to_i
        view = arr[2].strip.to_i
        time = arr[3].strip
        if uid!=nil && pid!=nil && rating!=nil
            rangeArr.each_with_index do |r,i|
                if i+1 < rangeArr.length
                    if rangeArr[i].to_i <= view && view < rangeArr[i+1].to_i
                        rating = ratingArr[i]
                        break
                    end
                else
                    rating = ratingArr[ratingArr.length-1]
                end
            end
            ratingOutputFile.write("#{uid.to_s}\t#{pid.to_s}\t#{rating.to_s}\t#{time}\n") if uid!=nil && pid!=nil
         end
    }
ensure
    sortedInputFile.close if sortedInputFile != nil
    ratingOutputFile.close if ratingOutputFile != nil
end

puts '[info] Job is done !!!'
